package com.clementheliou.cheliou.business.functional.member.impl;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clementheliou.cheliou.business.dataaccess.member.MemberDataAccessService;
import com.clementheliou.cheliou.business.dataaccess.member.exception.EMailAlreadyUsedException;
import com.clementheliou.cheliou.business.functional.member.MemberFunctionalService;
import com.clementheliou.cheliou.dal.model.member.Member;
import com.clementheliou.cheliou.dal.model.member.enumeration.MemberRole;

/**
 * Default implementation of the {@link MemberFunctionalService} interface.
 * <p>
 * Due to the {@link Transactional} annotation and the value of its
 * <i>readOnly</i> attribute, all methods are called in a transaction that will
 * be rollbacked in the end. This behavior can be overriden at the method level,
 * more particularly if you need to perform an insert, update or delete action.
 * </p>
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link MemberFunctionalService};
 * @see {@link Service};
 * @see {@link Transactional}.
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class MemberDefaultFunctionalService implements MemberFunctionalService {

	/**
	 * Data access service related to the marks.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Autowired
	private MemberDataAccessService memberDataAccessService;

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Transactional}.
	 * @since 1.0
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = { EMailAlreadyUsedException.class })
	public void addUserMember(Member memberToAdd) throws EMailAlreadyUsedException {

		final List<MemberRole> defaultRoles = new ArrayList<MemberRole>();
		defaultRoles.add(MemberRole.USER);

		memberToAdd.setRolesList(defaultRoles);
		memberToAdd.setSubscriptionDate(new LocalDate());

		memberDataAccessService.saveMember(memberToAdd);
	}

}
